<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="订单号" prop="orderNumber">
				<el-input v-model="queryParams.orderNumber" placeholder="请输入订单号" clearable />
			</el-form-item>
			<el-form-item label="市场" prop="courseDealerId">
				<el-select v-model="queryParams.courseDealerId" placeholder="请选择市场" clearable filterable>
					<el-option v-for="(item, index) in dealerlist" :key="index" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="联系人" prop="userName">
				<el-input v-model="queryParams.userName" placeholder="请输入联系人" clearable />
			</el-form-item>
			<el-form-item label="联系电话" prop="userPhone">
				<el-input v-model="queryParams.userPhone" placeholder="请输入联系电话" clearable />
			</el-form-item>
			<el-form-item label="省市区" prop="province">
				<el-cascader v-model="queryParams.area" :options="addressList" placeholder="请选择省市区" style="width: 100%;"></el-cascader>
			</el-form-item>
			<el-form-item label="地址" prop="address">
				<el-input v-model="queryParams.address" placeholder="请输入地址" clearable />
			</el-form-item>
			<el-form-item label="讲师" prop="courseLecturerId">
				<el-select v-model="queryParams.courseLecturerId" placeholder="请选择讲师" clearable filterable>
					<el-option v-for="(item, index) in Lecturerlist" :key="index" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="时间" prop="queryCreateTime">
				<el-date-picker v-model="queryParams.queryCreateTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"></el-date-picker>
			</el-form-item>
			<el-form-item label="付款状态" prop="paymentStatus">
				<el-select v-model="queryParams.paymentStatus" placeholder="请选择" clearable>
					<el-option label="全部" value=""></el-option>
					<el-option label="未支付" value="0"></el-option>
					<el-option label="已支付" value="1"></el-option>
					<el-option label="待核实" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="付款进度" prop="paymentSeep">
				<el-select v-model="queryParams.paymentSeep" placeholder="请选择" clearable>
					<el-option label="全部" value=""></el-option>
					<el-option label="未支付" value="0"></el-option>
					<el-option label="定金" value="1"></el-option>
					<el-option label="全款" value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="是否评论" prop="isComment">
				<el-select v-model="queryParams.isComment" placeholder="请选择" clearable>
					<el-option label="全部" value=""></el-option>
					<el-option label="未评论" value="0"></el-option>
					<el-option label="已评论" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="讲师付款" prop="isLecturerPaymentStatus">
				<el-select v-model="queryParams.isLecturerPaymentStatus" placeholder="请选择" clearable>
					<el-option label="全部" value=""></el-option>
					<el-option label="未付款" value="0"></el-option>
					<el-option label="已付款" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['course:order:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['course:order:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['course:order:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['course:order:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange" border>
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="订单号" align="center" prop="orderNumber" width="140px" />
			<el-table-column label="市场" align="center" prop="courseDealerName" width="100px" />
			<el-table-column label="联系人" align="center" prop="userName" width="80px" />
			<el-table-column label="联系电话" align="center" prop="userPhone" width="120px" />
			<el-table-column label="讲座地址" align="center">
				<template slot-scope="scope">
					{{ scope.row.provinceName + scope.row.cityName + scope.row.areaName + scope.row.address }}
				</template>
			</el-table-column>
			<el-table-column label="讲座时间" align="center" width="250px">
				<template slot-scope="scope">
					<div v-for="(time, index) in scope.row.orderTime" :key="index">
						{{ time.startTime }} - {{ time.endTime }} （{{ time.classHourType }}课时）
					</div>
				</template>
			</el-table-column>
			<!-- <el-table-column label="课时类型" align="center" width="100px">
				<template slot-scope="scope">
					{{ scope.row.classHourType == 1 ? '1课时' : '2课时' }}
				</template>
			</el-table-column> -->
			<el-table-column label="课时合计" align="center" prop="classHour" width="80px" />
			<el-table-column label="费用" align="center" prop="price" width="100px" />
			<el-table-column label="备注" align="center" prop="remarks" />
			<el-table-column label="讲座讲师" align="center" prop="courseLecturerName" width="80px" />
			<el-table-column label="支付凭证" align="center" width="160px">
				<template slot-scope="scope">
					<div style="display: flex;justify-content: center;">
						<el-image style="width: 70px; height: 70px" v-for="(voucher, i) in scope.row.voucher" :key="i" :src="voucher" fit="contain" :preview-src-list="scope.row.voucher"></el-image>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="付款状态" align="center" width="80px">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.payment_status" :value="scope.row.paymentStatus" />
				</template>
			</el-table-column>
			<el-table-column label="付款进度" align="center" width="80px">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.payment_seep" :value="scope.row.paymentSeep" />
				</template>
			</el-table-column>
			<el-table-column label="订单状态" align="center" width="80px">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.order_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="是否评论" align="center" prop="isComment" width="80px">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.order_is_comment" :value="scope.row.isComment"
						v-if="scope.row.isComment == 0" />
					<template v-else>
						<div>评分：{{ scope.row.comment.starRating }}</div>
						<div>评价：{{ scope.row.comment.content }}</div>
					</template>
				</template>
			</el-table-column>
			<el-table-column label="讲师付款" align="center" prop="isLecturerPaymentStatus" width="80px">
				<template slot-scope="scope">
					<el-switch v-model="scope.row.isLecturerPaymentStatus" :disabled="!(scope.row.paymentStatus == 1 && scope.row.paymentSeep == 2)"
						@change="updateIsLecturerPaymentStatus(scope.row)"></el-switch>
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-document" @click="handleExamine(scope.row)"
						v-hasPermi="['course:order:edit']"
						v-if="scope.row.paymentStatus == 2 || (scope.row.courseLecturerId == 0 && scope.row.status == 0)">审核</el-button>
					<el-button size="mini" type="text" icon="el-icon-edit" @click="editClassHour(scope.row)" v-hasPermi="['course:order:updateClassHour']" v-if="scope.row.courseLecturerId > 0">修改课时</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
			<el-form ref="form" :model="form" :rules="rulesOrder" label-width="80px">
				<el-form-item label="市场" prop="courseDealerId">
					<el-select v-model="form.courseDealerId" placeholder="请选择市场" filterable style="width: 100%;" @change="changeDealer">
						<el-option v-for="(item, index) in dealerlist" :key="index" :label="item.name" :value="item.id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="联系人" prop="userName">
					<el-input v-model="form.userName" placeholder="请输入联系人"></el-input>
				</el-form-item>
				<el-form-item label="联系电话" prop="userPhone">
					<el-input v-model="form.userPhone" placeholder="请输入联系电话"></el-input>
				</el-form-item>
				<el-form-item label="讲座地区" prop="areas">
					<el-cascader v-model="form.areas" :options="addressList" placeholder="请选择省市区" style="width: 100%;"></el-cascader>
				</el-form-item>
				<el-form-item label="讲座地址" prop="address">
					<el-input v-model="form.address" placeholder="请输入讲座地址"></el-input>
				</el-form-item>
				<el-form-item label="讲座老师" prop="courseLecturerId">
					<el-select v-model="form.courseLecturerId" placeholder="请选择讲师" filterable style="width: 100%;">
						<el-option v-for="(item, index) in Lecturerlist" :key="index" :label="item.name" :value="item.id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="课时" prop="classHour">
					<div v-for="(_, index) in form.orderTimeList" :key="index" class="mb10" style="display: flex;align-items: center;">
						<el-date-picker v-model="form.orderTimeList[index].time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" @change="orderTimeListChange" :data-index="index"></el-date-picker>
						<div style="margin-left: 10px;">
							<el-radio v-model="form.orderTimeList[index].classHourType" :label="1">1课时</el-radio>
							<el-radio v-model="form.orderTimeList[index].classHourType" :label="2">2课时</el-radio>
						</div>
						<i class="el-icon-circle-plus-outline" style="margin-left: 10px;cursor: pointer;" @click="addTimeList" v-if="index == 0"></i>
						<i class="el-icon-remove-outline" style="margin-left: 10px;cursor: pointer;" @click="delTimeList(index)" v-else></i>
					</div>
				</el-form-item>
				<el-form-item label="备注" prop="remarks">
					<el-input
					  type="textarea"
					  :autosize="{ minRows: 5, maxRows: 10}"
					  placeholder="请输入备注"
					  v-model="form.remarks">
					</el-input>
				</el-form-item>
				<el-form-item label="付款状态" prop="classHourType">
					<el-radio-group v-model="form.paymentStatus">
						<el-radio :label="0">未付款</el-radio>
						<el-radio :label="1">已付款</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="付款进度" prop="classHourType">
					<el-radio-group v-model="form.paymentSeep">
						<el-radio :label="0">未付款</el-radio>
						<el-radio :label="1">定金</el-radio>
						<el-radio :label="2">全款</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="凭证">
					<el-upload
					  :action="updateUrl"
					  :headers="headers"
					  :file-list="fileList"
					  list-type="picture-card"
					  :limit="9"
					  :on-success="handleSuccess"
					  :on-remove="handleRemove">
					  <i class="el-icon-plus"></i>
					</el-upload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="open = false">取 消</el-button>
			</div>
		</el-dialog>

		<!-- 订单审核 -->
		<el-dialog title="订单审核" :visible.sync="openExamine" width="70%" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="讲师" prop="coursecturerId" v-if="LecturerShow">
					<el-select v-model="form.coursecturerId" placeholder="请选择讲师" filterable style="width: 100%;">
						<el-option v-for="(item, index) in Lecturerlist" :key="index" :label="item.name" :value="item.id"></el-option>
					</el-select>
				</el-form-item>
				<template v-if="!LecturerShow">
					<el-form-item label="付款进度" prop="paymentSeep" v-if="paymentSeepShow">
						<el-radio-group v-model="form.paymentSeep">
							<el-radio :label="1">定金</el-radio>
							<el-radio :label="2">全款</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="付款状态" prop="paymentStatus">
						<el-radio-group v-model="form.paymentStatus">
							<el-radio :label="1">已付款</el-radio>
							<el-radio :label="0">未付款</el-radio>
						</el-radio-group>
					</el-form-item>
				</template>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitExamine">确 定</el-button>
				<el-button @click="openExamine = false">取 消</el-button>
			</div>
		</el-dialog>
		
		<!-- 修改课时 -->
		<el-dialog title="修改课时" :visible.sync="openClassHour" width="70%" append-to-body>
			<el-form ref="form" label-width="80px">
				<template>
					<el-form-item label="课时" prop="classHour">
						<div v-for="(_, index) in orderTimeList" :key="index" class="mb10" style="display: flex;align-items: center;">
							<el-date-picker v-model="orderTimeList[index].time" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"></el-date-picker>
							<div style="margin-left: 10px;">
								<el-radio v-model="orderTimeList[index].classHourType" :label="1">1课时</el-radio>
								<el-radio v-model="orderTimeList[index].classHourType" :label="2">2课时</el-radio>
							</div>
							<i class="el-icon-remove-outline" style="margin-left: 10px;cursor: pointer;" @click="delTimeList(index)" v-if="index > 0"></i>
						</div>
					</el-form-item>
					<el-form-item label="凭证">
						<el-upload
						  :action="updateUrl"
						  :headers="headers"
						  :file-list="fileList"
						  list-type="picture-card"
						  :limit="9"
						  :on-success="handleSuccess"
						  :on-remove="handleRemove">
						  <i class="el-icon-plus"></i>
						</el-upload>
					</el-form-item>
				</template>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitClassHour">确 定</el-button>
				<el-button @click="openClassHour = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listOrder,
		getOrder,
		delOrder,
		addOrder,
		updateOrder,
		updateClassHour
	} from "@/api/course/order";
	import {
		listLecturer
	} from "@/api/course/lecturer"
	import {
		listDealer
	} from "@/api/course/dealer"
	import {
		addressList
	} from '@/utils/cityData.js'
	import { getToken } from '@/utils/auth'
	export default {
		name: "Order",
		dicts: ['payment_status', 'payment_seep', 'order_status', 'lecturer_payment_status', 'order_is_comment'],
		data() {
			return {
				id: 0,
				classHourType: 1,
				orderTimeList: [],
				addressList,
				updateUrl: process.env.VUE_APP_BASE_API + '/common/upload',
				headers: {
					Authorization: `Bearer ` + getToken()
				},
				fileList: [],
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 约课-订单表格数据
				orderList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				openExamine: false,
				openClassHour: false,
				Lecturerlist: [],
				dealerlist: [],
				LecturerShow: false,
				paymentSeepShow: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					courseDealerId: null,
					province: null,
					city: null,
					area: null,
					address: null,
					courseLecturerId: null,
					userName: null,
					userPhone: null,
					voucher: null,
					paymentStatus: null,
					status: null,
					isComment: null,
					isLecturerPaymentStatus: null,
					isDel: null,
					queryCreateTime: [],
					paymentSeep: null,
				},
				// 表单参数
				form: {
					classHourType: 1,
					coursecturerId: 0,
					paymentSeep: 0,
					paymentStatus: 0
				},
				// 表单验证
				rulesOrder: {
					courseDealerId: [{ required: true, message: '请选择市场', trigger: 'change' }],
					userName: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
					userPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
					areas: [{ required: true, message: '请输入讲座地区', trigger: 'change' }],
					address: [{ required: true, message: '请输入讲座地址', trigger: 'blur' }],
					courseLecturerId: [{ required: true, message: '请选择讲师', trigger: 'change' }]
				},
				// 表单校验
				rules: {
					coursecturerId: [{
						required: true,
						message: '请选择讲师',
						trigger: 'change'
					}],
					paymentStatus: [{
						required: true,
						message: '请选择付款状态',
						trigger: 'change'
					}],
				}
			};
		},
		created() {
			console.log('updateUrl', this.updateUrl);
			this.getList();

			listDealer({pageSize: 100}).then(res => {
				if (res.code == 200) {
					this.dealerlist = res.rows;
				}
			});

			listLecturer().then(res => {
				if (res.code == 200) {
					this.Lecturerlist = res.rows;
				}
			});
		},
		methods: {
			orderTimeListChange(e){
				console.log('orderTimeListChange', e);
			},
			changeDealer(e) {
				console.log('市场修改', e);
				let dealer = this.dealerlist.filter(item => item.id == e);
				this.form.userName = dealer[0].userName;
				this.form.userPhone = dealer[0].userPhone;
			},
			/**
			 * 删除图片
			 * @param {Object} e
			 */
			handleRemove(e){
				this.fileList = this.fileList.filter(item => item.url != e.url);
			},
			/**
			 * 上传图片
			 * @param {Object} e
			 */
			handleSuccess(e){
				this.fileList.push({url: e.url});
			},
			/**
			 * 增加课时
			 */
			addTimeList() {
				this.form.orderTimeList.push({time: [], classHourType: 1});
			},
			/**
			 * 删除日期
			 * @param {Object} index
			 */
			delTimeList(index){
				let orderTimeList = this.form.orderTimeList.filter((t, i) => i != index);
				this.form.orderTimeList = orderTimeList;
			},
			/**
			 * 修改课时
			 */
			editClassHour(row){
				console.log('修改课时', row);
				this.orderTimeList = [];
				getOrder(row.id).then(response => {
					response.data.orderTime.forEach(item => {
						this.orderTimeList.push({time: [item.startTime, item.endTime], classHourType: item.classHourType});
					});
					
					this.id = row.id;
					this.classHourType = row.classHourType;
					this.fileList = [];
					
					row.voucher.forEach(img => {
						this.fileList.push({url: img});
					});
					
					this.openClassHour = true;
				});
			},
			/**
			 * 修改课时-提交
			 */
			submitClassHour(){
				let orderTime = [];
				
				this.orderTimeList.forEach(item => {
					orderTime.push({startTime: item.time[0], endTime: item.time[1], classHourType: item.classHourType});
				});
				
				updateClassHour({
					id: this.id,
					orderTime: orderTime
				}).then(res => {
					if (res.code == 200) {
						this.$modal.msgSuccess("修改成功");
						this.openClassHour = false;
						this.getList();
					}else{
						this.$modal.msgError(res.msg);
					}
				});
			},
			/**
			 * 修改讲师付款状态
			 * @param {Object} row
			 */
			updateIsLecturerPaymentStatus(row) {
				updateOrder({
					id: row.id,
					paymentStatus: row.paymentStatus,
					isLecturerPaymentStatus: row.isLecturerPaymentStatus ? 1 : 0,
				}).then(res => {
					if (res.code == 200) {
						this.$modal.msgSuccess("修改成功");
						this.getList();
					}
				});
			},
			/** 查询约课-订单列表 */
			getList() {
				this.loading = true;
				this.orderList = [];
				listOrder(this.queryParams).then(response => {
					response.rows.forEach(item => {
						item.isLecturerPaymentStatus = item.isLecturerPaymentStatus == 0 ? false : true;
						item.voucher = item.voucher ? item.voucher.split(',') : [];
						item.voucher.map(img => {
							if(img.includes("bpm.app.edongfanghong.com")){
								img.replace('http', 'https');
							}
						})
						this.orderList.push(item);
					})

					// this.orderList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					courseDealerId: null,
					province: null,
					city: null,
					area: null,
					areas: [],
					address: null,
					courseLecturerId: null,
					userName: null,
					userPhone: null,
					voucher: null,
					paymentStatus: null,
					status: null,
					isComment: null,
					isDel: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null,
					classHourType: 1,
					paymentStatus: 0,
					paymentSeep: 0,
					orderTimeList: [{time: [], classHourType: 1}],
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				if (this.queryParams.area && this.queryParams.area.length > 0) {
					this.queryParams.province = this.queryParams.area[0] + '0000';
					this.queryParams.city = this.queryParams.area[1] + '00';
					this.queryParams.area = this.queryParams.area[2];
				}
				
				if(this.queryParams.queryCreateTime && this.queryParams.queryCreateTime.length > 0){
					this.queryParams.params = {};
					this.queryParams.params['beginStartTime'] = this.queryParams.queryCreateTime[0];
					this.queryParams.params['endStartTime'] = this.queryParams.queryCreateTime[1];
				}

				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加约课-订单";
			},
			/**
			 * 审核
			 * @param {Object} row
			 */
			handleExamine(row) {
				getOrder(row.id).then(response => {
					this.form = response.data;
					if (response.data.courseLecturerId <= 0) {
						this.form.courseLecturerId = '';
						this.LecturerShow = true;
					} else {
						this.LecturerShow = false;
						this.form.paymentStatus = 1;
						if(response.data.paymentSeep == 0){
							this.paymentSeepShow = true;
							this.form.paymentSeep = 2;
						}else{
							this.paymentSeepShow = false;
						}
					}

					this.openExamine = true;
				});
			},
			/**
			 * 提交审核
			 */
			submitExamine() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						let data = {
							id: this.form.id,
							paymentSeep: !this.LecturerShow && !this.paymentSeepShow && this.form.paymentStatus == 1 ? 2 : this.form.paymentSeep,
							coursecturerId: this.form.coursecturerId,
							paymentStatus: this.form.paymentStatus
						};

						if (this.form.paymentStatus == 1) {
							data.status = 1;
						}

						updateOrder(data).then(res => {
							if (res.code == 200) {
								this.$modal.msgSuccess("审核成功");
								this.openExamine = false;
								this.getList();
							}
						});
					}
				});
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.fileList = [];
				this.reset();
				const id = row.id || this.ids
				getOrder(id).then(response => {
					let data = response.data;
					
					this.form.id = data.id;
					this.form.courseDealerId = data.courseDealerId;
					this.form.province = data.province;
					this.form.city = data.city;
					this.form.area = data.area;
					this.form.address = data.address;
					this.form.courseLecturerId = data.courseLecturerId;
					this.form.userName = data.userName;
					this.form.userPhone = data.userPhone;
					this.form.paymentStatus = data.paymentStatus;
					this.form.status = data.status;
					this.form.isComment = data.isComment;
					this.form.isDel = data.isDel;
					this.form.createBy = data.createBy;
					this.form.createTime = data.createTime;
					this.form.updateBy = data.updateBy;
					this.form.updateTime = data.updateTime;
					this.form.classHourType = data.classHourType;
					this.form.paymentStatus = data.paymentStatus;
					this.form.paymentSeep = data.paymentSeep;
					this.form.voucher = data.voucher;
					this.form.areas = [this.form.province.toFixed().slice(0, 2), this.form.city.toFixed().slice(0, 4), this.form.area.toFixed()];
					this.form.orderTimeList = [];
					data.orderTime.forEach(time => {
						this.form.orderTimeList.push({time: [time.startTime, time.endTime], classHourType: time.classHourType});
					});
					
					if(this.form.voucher){
						this.form.voucher.split(',').forEach(img => {
							this.fileList.push({url: img});
						});
					}
					
					
					this.open = true;
					this.title = "修改约课-订单";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						this.form.province = this.form.areas[0] + '0000';
						this.form.city = this.form.areas[1] + '00';
						this.form.area = this.form.areas[2];
						console.log('提交按钮', this.form);
						
						let timeVide = true;
						let overTime = false;
						let OrderTime = [];
						this.form.orderTimeList.forEach(time => {
							if(time.time.length == 0){
								timeVide = false;
							}else{
								OrderTime.push({startTime: time.time[0], endTime: time.time[1], classHourType: time.classHourType});
								if((new Date(time[0])).getTime() >=  Date.now()){
									overTime = true;
								}
							}
						});
						
						this.form.orderTime = OrderTime;
						
						if(!timeVide){
							this.$modal.msgError("请选择课时！");
						}else if(this.form.paymentStatus == 1 && this.fileList.length == 0){
							this.$modal.msgError("请上传凭证！");
						}else{
							
							this.form.voucher = this.fileList.map(file => file.url).join(',');
							if(this.form.paymentStatus == 1){
								this.form.status = overTime ? 2 : 1;
							}
							
							if (this.form.id != null) {
								updateOrder(this.form).then(res => {
									if(res.code == 200){
										this.$modal.msgSuccess("修改成功");
										this.open = false;
										this.getList();
									}else{
										this.$modal.msgError(res.msg);
									}
								});
							} else {
								addOrder(this.form).then(res => {
									if(res.code == 200){
										this.$modal.msgSuccess("新增成功");
										this.open = false;
										this.getList();
									}else{
										this.$modal.msgError(res.msg);
									}
								});
							}
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除约课-订单编号为"' + ids + '"的数据项？').then(function() {
					return delOrder(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('course/order/customExport', {
					...this.queryParams
				}, `约课订单列表_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>